package com.haust.su.ghyy.cmn.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haust.su.ghyy.cmn.listener.DictListener;
import com.haust.su.ghyy.cmn.mapper.DictMapper;
import com.haust.su.ghyy.cmn.service.DictService;
import com.haust.su.ghyy.model.cmn.Dict;
import com.haust.su.ghyy.vo.cmn.DictEeVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author haustsusu
 * @create 2023-02-13-13:53
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {



    //根据数据字典的id查询子数据列表
    @Override
    //缓存的注解
    @Cacheable(value = "dict",keyGenerator = "keyGenerator")
    public List<Dict> findChildrenDate(Long id) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<Dict> list = baseMapper.selectList(queryWrapper);

        //在list集合中为每一个dict对象中设置hashChild
        for (Dict dict:list){
            Long id1 = dict.getId();
            Boolean aBoolean = this.ifHaveChildren(id1);
            dict.setHasChildren(aBoolean);
        }

        return list;
    }

    //导出数据字典接口操作
    @Override
    public void exportDictData(HttpServletResponse httpServletResponse) {
        //设置下载的信息
        httpServletResponse.setContentType("application/vnd.ms-excel");
        httpServletResponse.setCharacterEncoding("utf-8");
        String fileName = "dict";
        httpServletResponse.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

        //查询数据库
        List<Dict> Dictlist = baseMapper.selectList(null);

        //需要将Dict对象集合转为DictEeVo集合
        //创建DictEeVo集合
        List<DictEeVo> dictEeVoList=new ArrayList<>();

        for (Dict dict:Dictlist){
            DictEeVo dictEeVo=new DictEeVo();
            //下面两种第一种只将id获取
            //第二种将dictEeVo中所有内容获取
            dictEeVo.setId(dict.getId());
            dictEeVo.setDictCode(dict.getDictCode());
            dictEeVo.setName(dict.getName());
            dictEeVo.setParentId(dict.getParentId());
            dictEeVo.setValue(dict.getValue());
            //BeanUtils.copyProperties(dict,dictEeVo);
            dictEeVoList.add(dictEeVo);

        }

        //调用easyExcel中的方法进行写操作
        //写入dowrite的list集合需要与DictEoVo实体类一致
        try {
            EasyExcel.write(httpServletResponse.getOutputStream(), DictEeVo.class).sheet("dict").doWrite(dictEeVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //导入数据字典接口
    @Override
    @CacheEvict(value = "dict",allEntries = true)
    public void importDictData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),DictEeVo.class,new DictListener(baseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据dictcode和value查询
    @Override
    public String getDictName(String dictCode, String value) {
        //如果dictcode为空,直接查value
        if (StringUtils.isEmpty(dictCode)) {
            //直接查询value
            QueryWrapper<Dict> wrapper = new QueryWrapper<>();
            wrapper.eq("value", value);
            Dict dict = baseMapper.selectOne(wrapper);
            return dict.getName();
        }else {//如果dictcode不为空,根据dictCode
            //根据dictcode查询dict对象,得到dict的id值
            QueryWrapper<Dict> wrapper = new QueryWrapper<>();
            wrapper.eq("dict_code", dictCode);
            Dict codedict = baseMapper.selectOne(wrapper);
            long parent_id=codedict.getId();
            //根据parent_id与value进行查询
            Dict finalDict=baseMapper.selectOne(new QueryWrapper<Dict>().eq("parent_id",parent_id).eq("value",value));
            return finalDict.getName();

        }
    }

    //根据dictcode获取下级节点
    @Override
    public List<Dict> findByDictCode(String dictCode) {
        //根据dictcode获取对应的id
        //根据dictcode查询dict对象,得到dict的id值
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_code", dictCode);
        Dict codedict = baseMapper.selectOne(wrapper);

        List<Dict> listdate = this.findChildrenDate(codedict.getId());
        return listdate;
    }


    //写一个方法用来判断该id该节点下有没有子节点
    private Boolean ifHaveChildren(Long id){
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        Long count = baseMapper.selectCount(queryWrapper);
        return count>0;
    }
}
